package com.ops.uitl.data;

import java.sql.ResultSet;
import java.sql.Types;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import net.sf.json.JSONSerializer;

public class DataHelper {

	protected static Logger log = Logger.getLogger(DataHelper.class);
	
	/**
	 * str to list
	 * @param value
	 * @return List
	 */
	public static List strToList(String value) {
		if (value == null) return null;
		
		String orgstr = value;
		
		Pattern pattern = Pattern.compile("([\\[|\\{]+\")");
		Matcher matcher = pattern.matcher(value);
		boolean iswadestr = !(matcher.find() && value.startsWith(matcher.group()));
		
		StringBuffer str = new StringBuffer();
		String regstr = iswadestr ? "(\\r)|(\\n)|(\\$)|(\\\\)|(\", \")|([\\[(, )]?\"[\\](, )]?)" : "(\\r)|(\\n)|(\\$)|(\\\\)|([\\{,]\"\\d*\\w*[:]?\\w*\":)";
		pattern = Pattern.compile(regstr);
		matcher = pattern.matcher(value);
		while (matcher.find()) {
			String group = matcher.group();
			if ("\r".equals(group)) {
				matcher.appendReplacement(str, "!~5~!");
			} else if ("\n".equals(group)) {
				matcher.appendReplacement(str, "!~6~!");
			} else if ("$".equals(group)) {
				matcher.appendReplacement(str, "!~7~!");
			} else if ("\"".equals(group)) {
				matcher.appendReplacement(str, "!~8~!");
			} else if ("\\".equals(group)) {
				matcher.appendReplacement(str, "!~9~!");
			} else if (!iswadestr) {
				if ((group.startsWith("{\"") || group.startsWith(",\"")) && group.endsWith("\":")) {
					matcher.appendReplacement(str, group.replaceAll("\"", "!~a~!"));
				}
			}
		}
		matcher.appendTail(str);
		
		value = str.toString();
		str = new StringBuffer();
		pattern = Pattern.compile(iswadestr ? "(\".*?\"[\\],])" : "(\".*?\"[\\]\\},])");
		matcher = pattern.matcher(value);
		while (matcher.find()) {
			String group = matcher.group();
			String prefix = group.substring(0, 1);
			String suffix = group.substring(group.length() - 2);
			group = group.substring(1, group.length() - 2);
			
			StringBuffer substr = new StringBuffer();
			Pattern subpattern = Pattern.compile("(\\{)|(\\[)|(\\])|(,)|(\")");
			Matcher submatcher = subpattern.matcher(group);
			while (submatcher.find()) {
				String subgroup = submatcher.group();
				if ("{".equals(subgroup)) {
					submatcher.appendReplacement(substr, "!~1~!");
				} else if ("[".equals(subgroup)) {
					submatcher.appendReplacement(substr, "!~2~!");
				} else if ("]".equals(subgroup)) {
					submatcher.appendReplacement(substr, "!~3~!");
				} else if (",".equals(subgroup)) {
					submatcher.appendReplacement(substr, "!~4~!");
				} else if ("\"".equals(subgroup)) {
					submatcher.appendReplacement(substr, "!~8~!");
				}
			}
			submatcher.appendTail(substr);
			matcher.appendReplacement(str, prefix + substr + suffix);
		}
		matcher.appendTail(str);
		
		if (iswadestr) {
			value = str.toString();
			str = new StringBuffer();
			pattern = Pattern.compile("(=?[\\{\\[][\\{\\}\\[\\]]*(, [\"]?)?[\\{\\}\\[]*)|([\\}\\]]*(, [\"]?)[\\{\\[]*)|(\", \")");
			matcher = pattern.matcher(value);
			while (matcher.find()) {
				String group = matcher.group();
				if (group.startsWith("=")) {
					group = "\":" + group.substring(1);
				}
				if (group.endsWith("{")) {
					group += "\"";
				} else if (group.endsWith(" ")) {
					group += "\"";
				}
				group = group.replaceFirst(" ", "");
				matcher.appendReplacement(str, group);
			}
			matcher.appendTail(str);
			
			value = str.toString();
			str = new StringBuffer();
			pattern = Pattern.compile("(:\\[\".*?\"\\])");
			matcher = pattern.matcher(value);
			while (matcher.find()) {
				String group = matcher.group();
				if (!Pattern.compile("(\",\")|(\",\\[)").matcher(group).find()) {
					matcher.appendReplacement(str, group.substring(0, 1) + group.substring(2, group.length() - 1));	
				}
			}
			matcher.appendTail(str);
		}
		
		value = str.toString();
		str = new StringBuffer();
		pattern = Pattern.compile("(!~1~!)|(!~2~!)|(!~3~!)|(!~4~!)|(!~5~!)|(!~6~!)|(!~7~!)|(!~8~!)|(!~9~!)|(!~a~!)");
		matcher = pattern.matcher(value);
		while (matcher.find()) {
			String group = matcher.group();
			if ("!~1~!".equals(group)) {
				matcher.appendReplacement(str, "{");
			} else if ("!~2~!".equals(group)) {
				matcher.appendReplacement(str, "[");
			} else if ("!~3~!".equals(group)) {
				matcher.appendReplacement(str, "]");
			} else if ("!~4~!".equals(group)) {
				matcher.appendReplacement(str, ",");
			} else if ("!~5~!".equals(group)) {
				matcher.appendReplacement(str, "\\\\r");
			} else if ("!~6~!".equals(group)) {
				matcher.appendReplacement(str, "\\\\n");
			} else if ("!~7~!".equals(group)) {
				matcher.appendReplacement(str, "\\$");
			} else if ("!~8~!".equals(group)) {
				matcher.appendReplacement(str, "\\\\\"");
			} else if ("!~9~!".equals(group)) {
				matcher.appendReplacement(str, "\\\\\\\\");
			} else if (!iswadestr) {
				if ("!~a~!".equals(group)) {
					matcher.appendReplacement(str, "\"");
				}
			}
		}
		matcher.appendTail(str);
		value = str.toString();
		
		if (!(value.startsWith("[") && value.endsWith("]"))) {
			value = "[" + value + "]";
		}
		
		try {
			return (List) JSONSerializer.toJSON(value);
		} catch (Exception e) {
			log.error("original str:" + orgstr);
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * get result value
	 * 
	 * @param rs
	 * @param type
	 * @param name
	 * @return Object
	 * @throws Exception
	 */
	public static Object getValueByResultSet(ResultSet rs, int type, String name)
			throws Exception {
		if (type == Types.BLOB) {
			return rs.getBlob(name);
		} else {
			return rs.getString(name);
		}
	}
	
}